package com.ybkj.daijia.server.interceptors;

import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * @author zhyu
 * @date 2019/12/31 15:35
 */
public class LogInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        Enumeration<String> parameterNames = request.getParameterNames();
        StringBuilder sb = new StringBuilder("Method: ");
        sb.append(request.getMethod());
        sb.append(" Url:" + request.getRequestURI());
        sb.append(" Req[");
        while(parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            if (name.startsWith("base64") || name.endsWith("File")){
                continue;
            }
            String value = request.getParameter(name);
            if (parameterNames.hasMoreElements()) {
                sb.append(value + ",");
            }else{
                sb.append(value);
            }
        }
        sb.append("]");

        super.afterCompletion(request, response, handler, ex);
    }
}
